<?php

/**
 * This is the model class for table "tbl_contract_type".
 *
 * The followings are the available columns in table 'tbl_contract_type':
 * @property integer $id
 * @property string $description
 * @property integer $inactive
 * @property integer $sort
 */
class ContractType extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @return ContractType the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_contract_type';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('description, inactive, sort', 'required'),
            array('inactive, sort', 'numerical', 'integerOnly' => true),
            array('description', 'length', 'max' => 36),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, description, inactive, sort', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'description' => 'Description',
            'inactive' => 'Inactive',
            'sort' => 'Sort',
        );
    }

    public static function getListData($addAll = false)
    {
        $list = ContractType::model()->findAll('inactive=0');
        $rs = array();
        if($addAll){
            $rs[null] = 'All';
        }
        if (count($list) > 0) {
            foreach ($list as $item) {
                $rs[$item->id] = $item->description;
            }
        }
        return $rs;
    }
    public static function getText($id){
        $text=Yii::app()->db->createCommand('select description from tbl_contract_type where id=:id')
                ->bindValues(array('id'=>$id))->queryScalar();
        if($text!==false && $text!==NULL){
            return $text;
        }
        return '';
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('inactive', $this->inactive);
        $criteria->compare('sort', $this->sort);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
}